/**
 * 
 */
package com.manolo.server.dispatcher;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.manolo.server.event.ProcessEvent;
import com.manolo.server.listener.ProcessCommunicationListener;

/**
 * @author meyer
 * 
 */
public class ProcessCommunicationDispatcher {
	private static ProcessCommunicationDispatcher instance;

	private List<ProcessCommunicationListener> listeners;

	private ProcessCommunicationDispatcher() {
		this.listeners = new ArrayList<ProcessCommunicationListener>();
	}

	public void addProcessListener(
			ProcessCommunicationListener listener) {
		listeners.add(listener);
	}

	/**
	 * control singleton instance
	 * 
	 * @return
	 */
	public static ProcessCommunicationDispatcher getInstance() {
		if (instance == null) {
			instance = new ProcessCommunicationDispatcher();
		}
		return instance;
	}

	/**
	 * Dispatch event when server send message to client 
	 * 
	 * @param event
	 */
	public void dispatchSendeMessage(ProcessEvent event) {
		Iterator<ProcessCommunicationListener> serverComIterator = listeners
				.iterator();
		while (serverComIterator.hasNext()) {
			serverComIterator.next().onSendeMessage(event);
		}
	}
	
	/**
	 * Dispatch event when server close connection
	 * 
	 * @param event
	 */
	public void dispatchCloseConnection(ProcessEvent event) {
		Iterator<ProcessCommunicationListener> serverComIterator = listeners
				.iterator();
		while (serverComIterator.hasNext()) {
			serverComIterator.next().onCloseConnection(event);
		}
	}

	
}
